﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.IO;
using System.Web.Http.Description;
using ACServerApi.Hubs;
using ACServerApi.Models;
using ACServerApi.Extensions;
using TypeScriptProxyGenerator;

namespace ACServerApi.Controllers
{
    [TSGen(TSGenTag.ServerProxy)]
    [RoutePrefix("api/server")]
    public class ServerController : ApiControllerWithHub<ServerHub>
    {
        List<ACServer> servers = new List<ACServer>{};

        public ServerController()
        {
            var server = new ACServer() { Id = 0, Name = "TestServer1" };

            var cars = new List<ACCar> { new ACCar { Name = "abarth500" }, new ACCar { Name = "abarth500_s1" } };
            var tracks = new List<ACTrack> { new ACTrack { Name = "vallelunga-club", Config= "Test" } };
            server.Configurations.Add(new ACConfigServer { Name = "AC Test Server", Cars = cars, Tracks = tracks, PortHttp = 8098 });
            server.Configurations.Add(new ACConfigDynamicTrack());
            server.Configurations.Add(new ACConfigBooking());
            server.Configurations.Add(new ACConfigPractice());
            server.Configurations.Add(new ACConfigQualify());
            server.Configurations.Add(new ACConfigRace());

            servers.Add(server);
        }

        // GET api/server
        public IEnumerable<ACServer> Get()
        {
            return servers;
        }

        // GET api/server/1
        [Route("{id:int}", Name = "GetServerById")]
        [ResponseType(typeof(ACServer))]
        public ACServer GetServer(int id)
        {
            return servers.First(x => x.Id == id);
        }

        // GET api/server/name
        [Route("{name}", Name = "GetServerByName")]
        [ResponseType(typeof(ACServer))]
        public ACServer GetServer(string name)
        {
            return servers.First(x => x.Name == name);
        }

        // GET api/server/1/start
        [HttpGet]
        [Route("{id:int}/start")]
        [ResponseType(typeof(ACServer))]
        public ACServer StartServer(int id)
        {
            var server = servers.First(s => s.Id == id);
            server.Running = true;
            return server;
        }

        // GET api/server/1/setup
        [HttpGet]
        [Route("{id:int}/setup")]
        [ResponseType(typeof(ACServer))]
        public ACServer SetupServer(int id)
        {
            var server = servers.First(s => s.Id == id);

            using (TextWriter iniFile = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "test.ini", false))
            {
                iniFile.Write(server.SerializeToIni(false));
            }

            server.Running = true;
            return server;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Irgendwas kaputt machen
            }
            base.Dispose(disposing);
        }

    }
}
